<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the Domain class.
 *
 * PHP version 5
 *
 * This file is part of Majic - A rapid-development web application framework
 * for PHP 5.
 *
 * Majic is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Majic is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Majic. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Domain
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007-2008 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Domain.php 34 2008-07-19 04:50:53Z mjijackson $
 */

/**
 * Serves as an identity map for all objects in the domain. Also implements the
 * unit of work pattern to automate saving of modified objects.
 *
 * @package     Domain
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class Domain
{

    /**
     * Contains all listeners that have been registered to fire when domain
     * objects are saved.
     *
     * @var     array
     * @access  private
     * @static
     */
    private static $on_save_listeners = array(
        'all' => array()
    );

    /**
     * The domain store that will be used for all objects in the domain.
     *
     * @var     Domain_Store
     * @access  private
     * @static
     */
    private static $store;

    /**
     * Sets the domain store that will be used to store all domain objects.
     *
     * @param   Domain_Store    $store      The domain store to use
     * @access  public
     * @static
     */
    public static function setStore(Domain_Store $store)
    {
        self::$store = $store;
    }

    /**
     * Gets the current domain store. If none has been set, will automatically
     * set the domain store to an instance of Domain_Store_Process.
     *
     * @return  Domain_Store                The current domain store
     * @access  public
     * @static
     */
    public static function getStore()
    {
        if (!self::$store) {
            self::setStore(new Domain_Store_Process);
        }

        return self::$store;
    }

    /**
     * Unconditionally adds the given object to the domain map.
     *
     * @param   Domain_Object   $obj        The domain object
     * @return  void
     * @access  public
     * @static
     */
    public static function set(Domain_Object $obj)
    {
        $store = self::getStore();
        $store->set(self::globalKey($obj), $obj);
    }

    /**
     * Adds the given object to the domain map if it doesn't already exist.
     *
     * @param   Domain_Object   $obj        The domain object
     * @return  void
     * @access  public
     * @static
     */
    public static function add(Domain_Object $obj)
    {
        $store = self::getStore();
        $store->add(self::globalKey($obj), $obj);
    }

    /**
     * Finds an object with the given class and id in the domain map. If the
     * object cannot be found, a null value will be returned.
     *
     * @param   string      $obj_cls        The domain object class
     * @param   string      $id             The domain object id
     * @return  Domain_Object               The domain object with the given
     *                                      class and id
     * @access  public
     * @static
     */
    public static function find($obj_cls, $id)
    {
        $store = self::getStore();
        return $store->find($obj_cls . '::' . $id);
    }

    /**
     * Generates a global key that may be used to look up this object in the
     * domain map.
     *
     * @param   Domain_Object   $obj        The domain object
     * @return  string                      The domain object's global key
     * @access  public
     * @static
     */
    public static function globalKey(Domain_Object $obj)
    {
        return get_class($obj) . '::' . $obj->getId();
    }

    /**
     * Enables automatic saving of all domain objects.
     *
     * @return  void
     * @access  public
     * @static
     */
    public static function enableAutosave()
    {
        $store = self::getStore();
        $store->enableAutosave();
    }

    /**
     * Registers a callback function that will be fired whenever a domain object
     * is saved. If the $obj_cls parameter is provided, the callback will only
     * be fired for objects that are instances of that class. Otherwise, the
     * callback will be fired whenever any domain object is saved.
     *
     * @param   callback        $callback       The function to call
     * @param   string          $obj_cls        (optional) The class name to
     *                                          listen for
     * @access  public
     * @static
     */
    public static function registerOnSaveListener($callback, $obj_cls = 'all')
    {
        if (!is_array(self::$on_save_listeners[$obj_cls])) {
            self::$on_save_listeners[$obj_cls] = array();
        }

        self::$on_save_listeners[$obj_cls][] = $callback;
    }

    /**
     * Fires each of this object class' registered on-save listeners, passing
     * each the following parameters:
     *
     * - The domain object being saved
     * - The class name of the domain object
     *
     * @param   Domain_Object   $obj        The domain object being saved
     * @return  void
     * @access  public
     * @static
     */
    public static function fireOnSaveListeners(Domain_Object $obj)
    {
        foreach (self::$on_save_listeners as $obj_cls => $callbacks) {
            if ($obj_cls == 'all' || $obj instanceof $obj_cls) {
                foreach ($callbacks as $callback) {
                    call_user_func($callback, $obj, get_class($obj));
                }
            }
        }
    }

}

?>
